import { UserListParams } from "@/api/user/userModel";
import { reactive } from "vue";
import { getUserListApi } from "@/api/user/user";

export default function table() {
    // 定义表格绑定的数据
    const tableData = reactive({
        list: []
    })
    // 表格查询的参数
    const listQuery = reactive<UserListParams>({
        deptId: '',
        pageSize: 10,
        currentPage: 1,
        total: 0,
        loginName: ''
    })
    // 查询表格
    const getUserList = async () => {
        let res = await getUserListApi(listQuery)
        // console.log('表格加载完成===', res)
        if(res && res.code === 200) {
            tableData.list = res.data.records
            listQuery.total = res.data.total
        }
    }
    // 树组件调用,查询用户列表
    const treeClick = async (deptId:number) => {
        listQuery.deptId = deptId
        getUserList()
    }

    // 改变页容量
    const sizeChange =  (size:number) => {
        listQuery.pageSize = size
        getUserList()
    }

    // 页数改变
    const currentChange = async (page:number) => {
        listQuery.currentPage = page
        getUserList()
    }

    // 重置
    const resetBtn = async () => {
        listQuery.loginName = ''
        getUserList()
    }

    return {
        resetBtn,
        tableData,
        listQuery,
        getUserList,
        treeClick,
        sizeChange,
        currentChange
    }
}